package org.brisling.oa.queryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;

import org.brisling.base.domain.ExportExcelDomain;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.ExportUtil;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.StringUtil;
import org.brisling.oa.domain.OAAddressList;
import org.brisling.oa.domain.OAEquipmentPurchaseSmall;
import org.brisling.sysman.domain.UserPassword;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * <p>Title: QueryOAAddressListService</p>
 * <p>Description: 通讯录查询服务类</p>
 * <p>Company: 长沙东勤信息科技发展有限公司</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年8月17日 上午10:13:58
 */
@RestController
@RequestMapping("/oaAddressList/query")
public class QueryOAAddressListService extends AbstractBaseQueryService<OAAddressList>{
	
	
	@Autowired
	private QueryDictionaryService dicService;
	public QueryOAAddressListService(){
		this.moduleName = ConstSysParam.MODULE_OA_ADDRESS_LIST;
		this.subSystem = ConstSysParam.SUBSYSTEM_OA;
		this.tableName = ConstSysParam.VIEW_PREFIX + this.moduleName;
		this.clazz = OAAddressList.class;
		
		typeMap = this.getTypeMap(clazz);
		
		
		orderMap.put("sortedid","asc");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<OAAddressList> getListDomain(@RequestBody(required=false) String body){	
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	@RequestMapping("/getListDomainByDeptid.action")
	public ListDomain<OAAddressList> getListDomainByDeptid(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		if(deptid==null){
			ListDomain<OAAddressList> listDomain = new ListDomain<OAAddressList>();
			listDomain.setTotal(0l);
			listDomain.setRows(new ArrayList<OAAddressList>());
			return listDomain;
		}
		
		String sql = dicService.getSqlByBizcode("query_address");
		
		Map<String,String> pMap = new HashMap<String,String>();
		pMap.put("deptid", String.valueOf(deptid));
		
		sql = StringUtil.formatSql(pMap, sql);		
		
		return super.getPagedListDomain(page,rows,sql);
	}
	
	
	
	@RequestMapping("/getList.action")
	public List<OAAddressList> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public OAAddressList getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOneByIdentify("id", id.toString());		
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	
	public OAAddressList getOne(String id){
		return super.getOneByIdentify("id", id);		
	}
	
	/**
	 * 根据唯一编码查找业务记录
	 * @param identify	唯一编码
	 * @return 业务记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public OAAddressList getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	/**
	 * 根据指定字段名及字段值查找记录
	 * @param field		字段名
	 * @param fieldVal	字段值
	 * @return
	 */
	@RequestMapping("/getOneByBizTable.action")
	public OAAddressList getOneByBizTable(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByTable(field,fieldVal);
	}
	
	/**
	 * 根据部门id查找用户集合
	 * @param page	页码
	 * @param rows	行数
	 * @param deptid	部门编码
	 * @return	用户数据集
	 */
	@RequestMapping("/getUserByDeptid.action")
	public ListDomain<OAAddressList> getOAAddressListByDept(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Long deptid){
		
		
		String view_department = ConstSysParam.VIEW_PREFIX + ConstSysParam.MODULE_DEPARTMENT;
		
		String sql = " SELECT u.* FROM %1$s u LEFT JOIN %2$s d ON u.deptid=d.deptid AND d.deptid=%3$d" ;
		sql= String.format(sql, ConstSysParam.VIEW_USER, view_department,deptid);		
		
		return super.getPagedListDomain(page, rows, sql);
	}
	
	/**
	 * 获取用户密码
	 * @param yhid
	 * @return
	 */
	@RequestMapping("/getUserPassword.action")
	public UserPassword getUserPassword(@RequestParam(value="yhid",required=false)String yhid){
		
		String sql = "select * from %1$s where yhdm = '%2$s'";
		sql = String.format(sql, "V_MH_XTYH",yhid);
		Query qry = entityManager.createNativeQuery(sql); 
		return (UserPassword) super.reflectObjectMap(UserPassword.class, super.getSingalQueryResult(qry), null);
	}
	
	@RequestMapping("/exportExcel.action")
	public void exportExcel(@RequestBody(required=false) String body,HttpServletResponse response){
		ListDomain<OAAddressList> domains= this.getListDomain(body);
		String configStr = dicService.getSqlByBizcode("oa_addresslist_export");
		
		ObjectMapper mapper = JsonUtil.getMapper();
		JavaType _javaType = mapper.getTypeFactory().constructCollectionType(List.class, ExportExcelDomain.class);

		List<ExportExcelDomain> list = null;
		try {
			list =  mapper.readValue(configStr, _javaType);
		}catch(Exception e){
			e.printStackTrace();
		}
		ExportUtil<OAAddressList> eu = new ExportUtil<OAAddressList>("通讯录列表",list,domains.getRows());
		try {
			eu.exportByDomain(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
